package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.maintenancesystem.systemsetup;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.ConfigKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.MaintenanceSystemKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.SystemsetupKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨宇
 */
@Service
@Slf4j
public class SystemSetupSettingService extends BaseService {

	private static final Logger logger = LoggerFactory.getLogger(SystemSetupSettingService.class);

	@Resource
    HttpServletRequest httpServletRequest;
	HttpResponse httpResponse;

	/**
	 * @Author YangYu
	 * @Description 用户分页列表
	 * @Date Created in 11:39 2020/12/21
	 * @Param [jsonParam]
	 */
	public RespEntity userPagList(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userPagList","systemsetup");
	}
	/**
	 * @Author YangYu
	 * @Description 用户新增
	 * @Date Created in 11:06 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity userAdd(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userAdd","systemsetup");
	}

	/**
	 * @Author YangYu
	 * @Description 用户修改
	 * @Date Created in 11:07 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity userUpdate(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userUpdate","systemsetup");
	}

	public com.alibaba.fastjson.JSONObject wbRedirect(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		com.alibaba.fastjson.JSONObject jsonObject = restJavaReturn(jsonParam,"systemSetting","wbRedirect","systemsetup");
		return jsonObject;

	}

	/**
	 * @Author YangYu
	 * @Description 用户详情
	 * @Date Created in 11:11 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity userDetail(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userDetail","systemsetup");
	}

	/**
	 * @Author YangYu
	 * @Description 用户详情通过token
	 * @Date Created in 11:11 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity userDetailByToken(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userDetailByToken","systemsetup");
	}

	/**
	 * @Author YangYu
	 * @Description 用户删除
	 * @Date Created in 11:13 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity userDelete(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","userDelete","systemsetup");
	}

	/**
	 * @Author YangYu
	 * @Description 指派人员列表
	 * @Date Created in 10:03 2021/1/14
	 * @Param [jsonParam]
	 */

	public RespEntity assignPersonnelList(JSONObject jsonParam, HttpServletRequest httpServletRequest, HttpServletResponse response){
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","assignPersonnelList","systemsetup");
	}
	/**
	 * @Author YangYu
	 * @Description 角色列表
	 * @Date Created in 11:22 2021/1/6
	 * @Param [jsonParam]
	 */

	public RespEntity roleList(JSONObject jsonParam) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","roleList","systemsetup");
	}
	/**
	 * @Author LiHaoDong
	 * @Description 注册
	 * @Date Created in 11:06 2021/1/11
	 * @Param [jsonObject, httpServletRequest]
	 */

	public RespEntity regist(JSONObject jsonParam, HttpServletRequest httpServletRequest) {
		//处理支持form-data和json两种方式
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		return restJava(jsonParam,"systemSetting","regist","systemsetup");
	}
	/**
	 * 登录
	 * @param
	 * @return
	 */
	public RespEntity login(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		com.alibaba.fastjson.JSONObject jsonObject = restJavaReturn(jsonParam,"systemSetting","login","systemsetup");
		if (jsonObject.getString("code").equals("1")){
			return RespEntity.fail(jsonObject.getString("msg"));
		}
		return RespEntity.success(restJavaReturn(jsonParam,"systemSetting","login","systemsetup").getJSONObject("data"));
	}

	/**
	 * 刷新token
	 * @param
	 * @return
	 */
	public RespEntity refresh(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		String ip = ConfigKit.getStr("systemSetting","config");
		String methodPath = MaintenanceSystemKit.getStr("refresh","systemsetup");
		String domain = ip + methodPath;
        String xml = "";
        httpResponse = HttpRequest.post(domain)
				.header("Authorization", httpServletRequest.getHeader("Authorization"))
				.form(jsonParam)
				.execute();
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        logger.info(state+"--------------------------------------");
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
		if(jsonObject.getInteger("code")==1){
			return RespEntity.fail(jsonObject.getString("msg"));
		}
		logger.info(state+"--------------------------------------");
		if(jsonObject.get("data")!=null){
			com.alibaba.fastjson.JSONObject data = (com.alibaba.fastjson.JSONObject)jsonObject.get("data");
			if(data.get("token")!=null){
				Map<String, Object> map = new HashMap<>();
				map.put("token",data.get("token"));
				return RespEntity.success(map);
			}
		}
		return RespEntity.success();
	}

	/**
	 * 登出短token
	 * @param
	 * @return
	 */
	public RespEntity logout(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		String ip = ConfigKit.getStr("systemsetupJava","config");
		String methodPath = SystemsetupKit.getStr("logout","systemsetup");
		String domain = ip + methodPath;
		httpResponse = HttpRequest.delete(domain)
				.header("Authorization", httpServletRequest.getHeader("Authorization"))
				.form(jsonParam)
				.execute();
		int state = httpResponse.getStatus();
//		if(state == 200) return RespEntity.success();
//		return RespEntity.fail("登出短token失败");
		return RespEntity.success();
	}

	/**
	 * 登出长token
	 * @param
	 * @return
	 */
	public RespEntity logout2(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		logger.info("+++++++++dropoutToken+++++"+jsonParam);
		String ip = ConfigKit.getStr("systemsetupJava","config");
		String methodPath = SystemsetupKit.getStr("logout2","systemsetup");
		String domain = ip + methodPath;
		httpResponse = HttpRequest.delete(domain)
				.header("Authorization", httpServletRequest.getHeader("Authorization"))
				.body(jsonParam.toJSONString(1))
				.execute();
		int state = httpResponse.getStatus();
//		if(state == 200) return RespEntity.success();
//		return RespEntity.fail("登出长token失败");
		return RespEntity.success();
	}



}
